package com.mspbots.rule.queue.producer;

import com.mspbots.api.service.TenantService;
import com.mspbots.api.service.TenantUserTimeEntryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitMessagingTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

/**
 * @ClassName com.mspbots.rule.queue.producer.MessageProducer
 * @Description TODO
 * @Author Jun
 * @Date 2019/8/28 15:53
 * @Version 1.0
 **/
@Slf4j
@Component
public class TimeEntryProducer {

    @Autowired
    TenantUserTimeEntryService tenantUserTimeEntryService;
    @Autowired
    TenantService tenantService;
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RabbitMessagingTemplate messagingTemplate;

        /*@Scheduled(cron="40 0/1 * * * ?")
    public void produce() {
        log.info("TimeEntryProducer... {}", LocalDateTime.now());
        Map<String, Object> tenantParams = Map.of("statusId", 1);
        List<TenantDTO> tenantDTOS = redisTemplate.opsForList().range(REDIS_KEY_PREFIX_TENANT + "tenants", 0, -1);
        List<TenantDTO> tenantsActive = tenantDTOS.stream().filter(item -> item.getStatusId() == 1).collect(Collectors.toList());
        int count = 0;
        for (TenantDTO tenantDTO : tenantsActive) {
            log.debug("tenant [{}]", tenantDTO.getId());
            Map<String, Object> params = Map.of("tenantId", tenantDTO.getId(),
                    "enteredDateStart", LocalDateTime.of(2019, 11, 19, 0, 0, 0));

            Collection<TenantTimeEntryDTO> tenantUserTimeEntryList = tenantUserTimeEntryService.findForRule(params);


            if (!CollectionUtils.isEmpty(tenantUserTimeEntryList)) {
                log.debug("tenant [{}] time entry [{}]", tenantUserTimeEntryList.size());
                for (TenantTimeEntryDTO tenantTimeEntryDTO : tenantUserTimeEntryList) {
                    count++;
                    this.messagingTemplate.convertAndSend("msp.wise.entries", "", tenantTimeEntryDTO);
                }

            }


//            Map<String, Object> timeLogParams = Map.of(
//                    "tenantId", tenantDTO.getId(),
//                    "createDateStart", LocalDateTime.now().minusHours(24));
//
//            List<TenantTimeLogDTO> timeLogDTOList = tenantUserTimeLogService.findList(timeLogParams);
//
//
//            for (TenantTimeLogDTO tenantTimeLogDTO : timeLogDTOList) {
//                if (tenantTimeLogDTO.getEndDate() != null && tenantTimeLogDTO.getEndDate().plusHours(1).isAfter(LocalDateTime.now())) {
//                    continue;
//                }
//                this.messagingTemplate.convertAndSend("msp.rule.ai", "trigger.NotEnoughTimeLogged", tenantTimeLogDTO);
//            }
        }
        log.info("time entry producer count : {}", count);

    }*/


}
